package ${bao}.${serviceImplPackage};

import com.alibaba.fastjson.JSONObject;
import ${bao}.${entityPackage}.${entity};
import ${bao}.${repositoryPackage}.${entity}Repository;
import ${bao}.${servicePackage}.${entity}Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ${entity}ServiceImpl implements ${entity}Service {
    @Autowired
    ${entity}Repository ${entity?lower_case}Repository;

    @Override
    public List<${entity}> list() {
        return ${entity?lower_case}Repository.findAll();
    }

    @Override
    public JSONObject listbyid(int id) {
        JSONObject jsonObject=new JSONObject();
        try {
            ${entity} ${entity?lower_case} = ${entity?lower_case}Repository.getOne(id);
            <#list table.fields as field>
            jsonObject.put("${field.propertyName}",${entity?lower_case}.get${field.propertyName?cap_first}());
            </#list>
            return jsonObject;
        }catch (Exception e){
            jsonObject.put("bool",false);
            return jsonObject;
        }
    }

    @Override
    public JSONObject add${entity?lower_case}(${entity} ${entity?lower_case}) {
            JSONObject jsonObject=new JSONObject();
            try {
                jsonObject.put("bool",true);
                ${entity?lower_case}Repository.save(${entity?lower_case});
                return jsonObject;
            }catch (Exception e){
                jsonObject.put("bool",false);
                return jsonObject;
            }
    }

    @Override
    public JSONObject delete${entity?lower_case}(int id) {
        JSONObject jsonObject=new JSONObject();
        try {
            jsonObject.put("bool",true);
            ${entity?lower_case}Repository.deleteById(id);
            return jsonObject;
        }catch (Exception e){
            jsonObject.put("bool",false);
            return jsonObject;
        }
    }

    @Override
    public JSONObject update${entity?lower_case}(${entity} ${entity?lower_case}) {
        JSONObject jsonObject=new JSONObject();
        try{
            ${entity?lower_case}Repository.save(${entity?lower_case});
        <#list table.fields as field>
            jsonObject.put("${field.propertyName}",${entity?lower_case}.get${field.propertyName?cap_first}());
        </#list>
            return jsonObject;
        } catch (Exception e) {
            jsonObject.put("bool",false);
            return jsonObject;
        }
    }
}
